package com.example.demo.controller;
/*
 * 李洲 2020030654A1213
 * 2024.05.24
 */
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/products")
public class ProductController {

    // 通过路径变量获取产品 ID
    @GetMapping("/{id}")
    public String getProductById(@PathVariable("id") int productId) {
        return "Fetching product with ID: " + productId;
    }

    // 通过路径变量获取产品 ID 和类别
    @GetMapping("/{category}/{id}")
    public String getProductByCategoryAndId(@PathVariable("category") String category, @PathVariable("id") int productId) {
        return "Fetching product with ID: " + productId + " in category: " + category;
    }

    // 通过路径变量获取可选版本参数
    @GetMapping("/{id}/version/{version}")
    public String getProductByIdAndVersion(@PathVariable("id") int productId, @PathVariable(value = "version", required = false) String version) {
        if (version == null) {
            return "Fetching product with ID: " + productId + " with no specific version";
        }
        return "Fetching product with ID: " + productId + " and version: " + version;
    }

    // 通过路径变量获取多个参数
    @GetMapping("/{category}/{type}/{id}")
    public String getProductByCategoryTypeAndId(@PathVariable String category, @PathVariable String type, @PathVariable int id) {
        return "Fetching product with ID: " + id + ", category: " + category + ", and type: " + type;
    }
}
//基本路径变量使用：
//@GetMapping("/{id}")
//public String getProductById(@PathVariable("id") int productId) {
//    return "Fetching product with ID: " + productId;
//}

//多路径变量
//@GetMapping("/{category}/{id}")
//public String getProductByCategoryAndId(@PathVariable("category") String category, @PathVariable("id") int productId) {
//    return "Fetching product with ID: " + productId + " in category: " + category;
//}

